<?php

/**
 * @date        : 2017年8月30日
 * @author      : Jason
 * @copyright   : http://www.hoge.cn/
 * @description : 后台控制器基类
 */
namespace post\base;



use post\modules\user\models\Admin;
use post\modules\user\models\User;
use yii;
use yii\web\Controller;
use yii\helpers\ArrayHelper;
use yii\web\Response;


class BaseController extends Controller
{
    public $enableCsrfValidation = false;

    public $no_need_login;
    
    public $check_work;

   



    public function init()
    {
        parent::init();
        // 设置不需要登录的url
        $this->no_need_login = [
            'user/login/*',

            'post/post-show/*',
            'user/register/*',
            'user/user/user',
            'user/fan/fan-gcount',

        ];

    }

    // 前置
    public function beforeAction($action)
    {
         parent::actions($action);

        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH');
        header('Access-Control-Allow-Headers:X-Requested-With, accept, content-type');
        header('Access-Control-Allow-Credentials:true');//是否支持cookie跨域


        if (in_array($this->module->id . '/' . $this->id . '/' . $this->action->id, $this->no_need_login) || in_array($this->module->id . '/' . $this->id . '/*', $this->no_need_login)) {

            return true;
        }else{

            $user_id = $this->getUserId();


            if ($user_id==NULL) {
                echo "登陆失败!";
                die();
            }
        }

        return true;
    }





    protected function getUser()
    {
        $access_token = Yii::$app->request->post('access_token');
        if (empty($access_token)) {
            $access_token = Yii::$app->request->get('access_token');
        }

        return User::findIdentityByAccessToken($access_token);
    }

    protected function getUserId()
    {
        $access_token = Yii::$app->request->post('access_token');
        if (empty($access_token)) {
            $access_token = Yii::$app->request->get('access_token');
        }
        var_dump($access_token);
        $user = User::findIdentityByAccessToken($access_token);

        return $user ? $user->id : null;
    }

    public function json_ok($data)
    {
        $arr=[];
        $arr["status"]="200";
        $arr["data"]=$data;
        $arr["error"]=0;
        return json_encode($arr,JSON_UNESCAPED_SLASHES);
    }

    public function json_error($data)
    {
        $arr=[];
        $arr["status"]="200";
        $arr["data"]=$data;
        $arr["error"]=1;
        return json_encode($arr,JSON_UNESCAPED_UNICODE);
    }





    //获取用户信息
    public function  user_Info()
    {
        $session=Yii::$app->session;
        $user= $session->get("user");
        return  $user;
    }

    //判断是否登陆
    public function is_login()
    {
        $session=Yii::$app->session;
        $user= $session->get("user");
        if($user!=null){
            return  true;
        }else{
            return false;
        }
    }

    protected function jsonSuccess($data = [], $message = '', $code = 0, $share = array())
    {
        $message = $message ? $message : '调用成功';
        $this->jsonEncode(true, $data, $message, $code, $share);
    }

    protected function jsonEncode($status, $data = [], $message = '', $code = 0)
    {
        $status = boolval($status);
        $data = $data ? $data : ( object )array();
        $message = strval($message);
        $code = intval($code);

        $result = [
            'status' => $status,
            'code' => $code,
            'message' => $message,
            'data' => $data
        ];

        // 设置响应对象
        $response = Yii::$app->response;
        $response->format = Response::FORMAT_JSON;
        $response->data = $result;
    }

    //父级Redis 赋值的Redis
    public  function set_redis($key,$val){

        $redis = \Yii::$app->redis;
        $redis->setex($key, 864000, $val);
    }

    public  function  del_redis(){

    }
    public   function data_validate($arr)
    {
        $request = \Yii::$app->request;
        $data=[];
        foreach ($arr as $item) {
            if ($item != null) {
                if ($request->post($item) == null) {

                    return "false";
                }else{

                    $data[$item]=$request->post($item);
                }
            }

        }

        return $data;
    }




}